package com.myzl.web.controller.coal;

import com.myzl.coal.domain.DeviceGroup;
import com.myzl.coal.service.IDeviceGroupService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备组Controller
 *
 * @author myzl
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/coal/group")
public class DeviceGroupController extends BaseController
{
    @Autowired
    private IDeviceGroupService deviceGroupService;

    /**
     * 查询设备组列表
     */
    @PreAuthorize("@ss.hasPermi('coal:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceGroup deviceGroup)
    {
        startPage();
        List<DeviceGroup> list = deviceGroupService.selectDeviceGroupList(deviceGroup);
        return getDataTable(list);
    }

    /**
     * 导出设备组列表
     */
    @PreAuthorize("@ss.hasPermi('coal:group:export')")
    @Log(title = "设备组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceGroup deviceGroup)
    {
        List<DeviceGroup> list = deviceGroupService.selectDeviceGroupList(deviceGroup);
        ExcelUtil<DeviceGroup> util = new ExcelUtil<DeviceGroup>(DeviceGroup.class);
        util.exportExcel(response, list, "设备组数据");
    }

    /**
     * 获取设备组详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceGroupService.selectDeviceGroupById(id));
    }

    /**
     * 新增设备组
     */
    @PreAuthorize("@ss.hasPermi('coal:group:add')")
    @Log(title = "设备组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceGroup deviceGroup)
    {
        return toAjax(deviceGroupService.insertDeviceGroup(deviceGroup));
    }

    /**
     * 修改设备组
     */
    @PreAuthorize("@ss.hasPermi('coal:group:edit')")
    @Log(title = "设备组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceGroup deviceGroup)
    {
        return toAjax(deviceGroupService.updateDeviceGroup(deviceGroup));
    }

    /**
     * 删除设备组
     */
    @PreAuthorize("@ss.hasPermi('coal:group:remove')")
    @Log(title = "设备组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceGroupService.deleteDeviceGroupByIds(ids));
    }

    @GetMapping(value = "/getGroupInfo/{groupType}")
    public AjaxResult getGroupInfo(@PathVariable("groupType") Integer groupType)
    {
        return AjaxResult.success(deviceGroupService.selectDeviceGroupByType(groupType));
    }


    @GetMapping(value = "/getChildGroupInfo/{parentId}")
    public AjaxResult getChildGroupInfo(@PathVariable("parentId") Integer parentId)
    {
        return AjaxResult.success(deviceGroupService.selectDeviceGroupByParentId(parentId));
    }
}
